package com.luxiu.extensionlib.cardView

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.cardview.widget.CardView
import androidx.dynamicanimation.animation.DynamicAnimation.OnAnimationUpdateListener
import androidx.dynamicanimation.animation.SpringAnimation
import androidx.dynamicanimation.animation.SpringForce
import com.luxiu.extensionlib.R

open class AnimationCardView(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0,
) : CardView(context, attrs, defStyleAttr) {
    private var mSpringAnimationX: SpringAnimation? = null
    private var mSpringAnimationY: SpringAnimation? = null
    var isNoAnimation: Boolean = false
    private var mUpdateListener: OnAnimationUpdateListener? = null

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.AnimationCardView, defStyleAttr, -1)
        isNoAnimation = typedArray.getBoolean(R.styleable.AnimationCardView_noAnimation, false)
        typedArray.recycle()
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        if (isNoAnimation) {
            return
        }
        if (mSpringAnimationX == null) {
            mSpringAnimationX = SpringAnimation(this, SpringAnimation.SCALE_X)
        }
        if (mSpringAnimationY == null) {
            mSpringAnimationY = SpringAnimation(this, SpringAnimation.SCALE_Y)
        }
        mSpringAnimationX!!.setSpring(
            SpringForce()
                .setStiffness(SPRING_STIFFNESS)
                .setDampingRatio(SPRING_DAMPING_RATIO)
        )
        mSpringAnimationY!!.setSpring(
            SpringForce()
                .setStiffness(SPRING_STIFFNESS)
                .setDampingRatio(SPRING_DAMPING_RATIO)
        )
        mUpdateListener = OnAnimationUpdateListener { _, _, _ ->
            // 确保 X 和 Y 缩放比例一致
            this.scaleY = this.scaleX
        }

        if (!mSpringAnimationX!!.isRunning) {
            mSpringAnimationX!!.addUpdateListener(mUpdateListener)
        }
        if (!mSpringAnimationY!!.isRunning) {
            mSpringAnimationY!!.addUpdateListener(mUpdateListener)
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        if (isNoAnimation) {
            return
        }
        mSpringAnimationX?.let {
            it.removeUpdateListener(mUpdateListener)
            it.cancel()
        }
        mSpringAnimationY?.let {
            it.removeUpdateListener(mUpdateListener)
            it.cancel()
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (isNoAnimation || !isEnabled) {
            return super.onTouchEvent(event)
        }
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                mSpringAnimationX?.spring?.finalPosition = SCALE_RATIO
                mSpringAnimationY?.spring?.finalPosition = SCALE_RATIO
                mSpringAnimationX?.start()
                mSpringAnimationY?.start()
                return true
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                mSpringAnimationX?.spring?.finalPosition = 1f
                mSpringAnimationY?.spring?.finalPosition = 1f
                mSpringAnimationX?.start()
                mSpringAnimationY?.start()
            }
        }
        return super.onTouchEvent(event)
    }
    companion object {
        private const val SPRING_STIFFNESS = 400f
        private const val SPRING_DAMPING_RATIO = 1f
        private const val SCALE_RATIO = 0.95f
    }
}